package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntBindInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntBindInfo;
import cc.rengu.igas.cops.common.enums.CopsBindStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户绑定.
 */
public class CopsMchntBindInfoMapperImpl implements CopsMchntBindInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.BSPS_POOL_NAME);

    @Override
    public List<CopsMchntBindInfo> selectCopsMchntBindInfoListByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntBindInfoListByUserId");
        CopsMchntBindInfo mchntBindInfo = new CopsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindUserId(userId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, CopsMchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据用户标识<{}>获取商户绑定信息失败!", userId);
            return null;
        }
        return objectList.stream().map(item -> (CopsMchntBindInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 根据角色查询商户绑定信息
     *
     * @param instId  机构标识
     * @param mchntNo 用户标识
     * @return 商户绑定信息
     * @throws Exception 异常
     */
    @Override
    public List<CopsMchntBindInfo> selectCopsMchntBindInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntBindInfoByMchntNo");
        CopsMchntBindInfo mchntBindInfo = new CopsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindStatus(CopsBindStatusEnum.BIND.getStatus()); //绑定
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, CopsMchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号：<{}>获取商户绑定信息为空!", mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (CopsMchntBindInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public List<CopsMchntBindInfo> selectCopsMchntBindInfoByMchntNoAndChainMchntNo(String instId, String mchntNo, String chainMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntBindInfoByMchntNoAndChainMchntNo");
        CopsMchntBindInfo mchntBindInfo = new CopsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setChainMchntNo(chainMchntNo);
        mchntBindInfo.setBindStatus(CopsBindStatusEnum.BIND.getStatus()); //绑定
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, CopsMchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号：<{}>门店号：<{}>获取商户绑定信息为空!", chainMchntNo, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (CopsMchntBindInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public int insertCopsMchntBindInfo(CopsMchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCopsMchntBindInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("插入商户绑定关系表失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public CopsMchntBindInfo selectCopsMchntBindInfoListByUserIdAndMchntNo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntBindInfoListByUserIdAndMchntNo");
        CopsMchntBindInfo mchntBindInfo = new CopsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindUserId(userId);
        mchntBindInfo.setBindStatus(CopsBindStatusEnum.BIND.getStatus()); //绑定
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.error("根据用户Id<{}>，商户号：<{}>获取商户绑定信息为空!", userId, mchntNo);
            return null;
        }
        return mchntBindInfo;
    }

    @Override
    public int updateCopsMchntBindInfoByPrimaryKey(CopsMchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateCopsMchntBindInfoByPrimaryKey");
        mchntBindInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户绑定信息失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    public CopsMchntBindInfo selectMchntBindInfoByMchntNo(String instId, String bindMchntType, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        CopsMchntBindInfo mchntBindInfo = new CopsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindMchntType(bindMchntType);
        mchntBindInfo.setMchntNo(mchntNo);
        if ("00".equals(bindMchntType) || "10".equals(bindMchntType)) {
            /*连锁商户和普通商户 角色类型对应法人商户*/
            mchntBindInfo.setBindUserRole("00");
        } else if ("11".equals(bindMchntType)) {
            /*门店商户，对应店长*/
            mchntBindInfo.setBindUserRole("01");
        } else {
            /*其他商户类型角色暂不处理*/
            rglog.debug("商户角色类型不需要微信推送,mchntNo:<{}> 角色类型{}", mchntNo, bindMchntType);
            return null;
        }
        mchntBindInfo.setBindStatus("00");
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntBindInfoByPk");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.debug("获取商户绑定信息失败,mchntNo:<{}> bindMchntType{}", mchntNo, bindMchntType);
            return null;
        }
        return mchntBindInfo;
    }
}
